<?php
class ClientLogin extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('UserModel', 'user', TRUE);
	}
	
	public function index(){
		echo "this contain login action from client";
	}
	
	public function login(){
		if (!isset($_GET['username']) || !isset($_GET['password'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
		}else{
			$user = $this->user->validate_user_from_client($_GET['username'], $_GET['password']);
			if (!$user){
				$arr = array('status' => 'fail', 'message' => 'error message here');
				echo json_encode($arr);
			}else{
				if (isset($_COOKIE['ci_session'])){
//					unset($user->username);
//					unset($user->password);
//					unset($user->first_name);
//					unset($user->last_name);
//					unset($user->date_of_birth);
//					unset($user->address);
//					unset($user->phone_number);
//					unset($user->image);
					$arr = array('status' => 'success', 'user_info' => $user, 'ci_session' => ($_COOKIE['ci_session']));
					$json = json_encode($arr);
					echo $json;
					return;
				}else{
					$this->session->sess_destroy();
					$arr = array('status' => 'fail', 'message' => 'error message here');
					echo json_encode($arr);
					return;
				}
				
			}
		}
	}
	
	public function logout(){
		$this->session->sess_destroy();
	}
}